package com.jjyd.crlp.services.impl;

import com.jjyd.crlp.domain.entity.Users;
import com.jjyd.crlp.domain.repository.UsersRepo;
import com.jjyd.crlp.model.dto.mine.MineInfoDto;
import com.jjyd.crlp.model.dto.mine.ModificationUserInfoDto;
import com.jjyd.crlp.services.MineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MineServiceImpl implements MineService {

    @Autowired
    private UsersRepo usersRepo;

    /**
     * 通过userId获取用户的昵称
     *
     * @param userId 用户id
     * @return 返回用户信息
     */
    @Override
    public MineInfoDto getInfo(Integer userId) {
        Users users = usersRepo.getByRemoveFalseAndId(userId);
        String nickName = users.getNickName();

        MineInfoDto mineInfoDto = new MineInfoDto();
        mineInfoDto.setNickName(nickName);

        return mineInfoDto;
    }

    /**
     * 修改用户信息
     *
     * @param modificationUserInfoDto 用户的个人信息
     * @param userId                  用户的id
     * @return 返回用户的id
     */
    @Override
    public Integer modification(ModificationUserInfoDto modificationUserInfoDto, Integer userId) {
        Users user = usersRepo.getByRemoveFalseAndId(userId);
        user.setNickName(modificationUserInfoDto.getNickName());
        return usersRepo.save(user).getId();
    }
}
